package com.cloud.pharmaceuticalClient.service.Impl;

import com.cloud.pharmaceuticalClient.pojo.ProductPicture;
import com.cloud.pharmaceuticalClient.service.ProductPictureService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhozixiong
 * @date 2023/02/26
 */
@Service
@RequiredArgsConstructor
public class ProductPictureServiceImpl implements ProductPictureService {

    private final MongoTemplate mongoTemplate;

    @Override
    public List<ProductPicture> searchAllByDid(int did) {
        Query query = new Query(Criteria.where("did").is(did));
        return mongoTemplate.find(query, ProductPicture.class);
    }

    @Override
    public ProductPicture searchByDid(int did) {
        return searchAllByDid(did).get(0);
    }

    @Override
    public void insert(ProductPicture productPicture) {
        mongoTemplate.insert(productPicture);
    }

    @Override
    public void insert(int did, String url) {
        ProductPicture productPicture = new ProductPicture(did, url);
        insert(productPicture);
    }

    @Override
    public void delBydid(int did) {
        Query query = new Query(Criteria.where("did").is(did));
        mongoTemplate.remove(query, ProductPicture.class);
    }

    @Override
    public void delByUrl(String url) {
        Query query = new Query(Criteria.where("url").is(url));
        mongoTemplate.remove(query, ProductPicture.class);
    }
}
